package com.maya.medicalwastes;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;


import com.kyleduo.switchbutton.SwitchButton;
import com.maya.medicalwastes.httpNet.common.SharedPreferencesConstant;
import com.maya.medicalwastes.util.ApiConfigConstant;
import com.maya.medicalwastes.util.PrefUtils;
import com.maya.medicalwastes.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * *
 */
public class SettingActivity extends AppCompatActivity {

    @BindView(R.id.et_ip_input)
    EditText et_ip_input;
    @BindView(R.id.et_port_input)
    EditText et_port_input;
    @BindView(R.id.btn_if_quiet)
    SwitchButton btnIfQuiet;
    @BindView(R.id.btn_if_scope)
    SwitchButton btnIfScope;
    @BindView(R.id.btn_ok)
    Button btn_ok;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);

        et_ip_input.setText(ApiConfigConstant.DEBUG_MODE ? ApiConfigConstant.DEBUG_IP : ApiConfigConstant.BASE_IP);
        et_port_input.setText(ApiConfigConstant.DEBUG_MODE ? ApiConfigConstant.DEBUG_PORT : ApiConfigConstant.BASE_PORT);

        btnIfScope.setChecked(PrefUtils.getBoolean(SettingActivity.this, SharedPreferencesConstant.IS_OPEN_PUNCH_SCOPE, true));
        btnIfQuiet.setChecked(PrefUtils.getBoolean(SettingActivity.this, SharedPreferencesConstant.IS_QUIET_LOGIN, false));

    }

    private void saveConfig() {
        //更改ip+port
        if (ApiConfigConstant.DEBUG_MODE) {
            //debug
            ApiConfigConstant.DEBUG_IP = et_ip_input.getText().toString();
            ApiConfigConstant.DEBUG_PORT = et_port_input.getText().toString();
            ApiConfigConstant.DEBUG_URL = ApiConfigConstant.HTTP + ApiConfigConstant.DEBUG_IP + ":" + ApiConfigConstant.DEBUG_PORT;
            // saveConfigToLocal(ApiConfigConstant.DEBUG_IP, ApiConfigConstant.DEBUG_PORT);
        } else {
            //release
            ApiConfigConstant.BASE_IP = et_ip_input.getText().toString();
            ApiConfigConstant.BASE_PORT = et_port_input.getText().toString();
            ApiConfigConstant.BASE_URL = ApiConfigConstant.HTTP + ApiConfigConstant.BASE_IP + ":" + ApiConfigConstant.BASE_PORT;
            // saveConfigToLocal(ApiConfigConstant.BASE_IP, ApiConfigConstant.BASE_PORT);
        }
        ApiConfigConstant.URL = ApiConfigConstant.DEBUG_MODE ? ApiConfigConstant.DEBUG_URL : ApiConfigConstant.BASE_URL;
        App.initRetrofit(getApplicationContext());

        //更改是否打开范围限制
        PrefUtils.putBoolean(SettingActivity.this, SharedPreferencesConstant.IS_OPEN_PUNCH_SCOPE, btnIfScope.isChecked());
        //更改静默模式
        PrefUtils.putBoolean(SettingActivity.this, SharedPreferencesConstant.IS_QUIET_LOGIN, btnIfQuiet.isChecked());

        ToastUtils.ViewToast(SettingActivity.this, "修改完成");
        SettingActivity.this.finish();
    }

    /*private void saveConfigToLocal(String ip, String port) {
        SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("ip", ip);
        editor.putString("port", port);
        editor.apply();
    }*/

    @OnClick({R.id.btn_if_quiet, R.id.btn_if_scope, R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_if_quiet:
                break;
            case R.id.btn_if_scope:
                break;
            case R.id.btn_ok:
                saveConfig();
                break;
        }
    }
}
